home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / Context.java < prev    next >
Text File  |  1998-08-21  |  4KB  |  154 lines

  1. package symantec.itools.lang;
  2.  
  3.  
  4. import java.applet.Applet;
  5. import java.net.URL;
  6.  
  7.  
  8. //  05/21/97    CAR changed initializeApp to set the document base URL to a valid URL
  9. //  05/15/97    CAR Added special handling to setDocumentBase when running inside of Netscape
  10. //                  for Windows off of a local drive.
  11.  
  12. /**
  13.  * This class tracks information relating to the Browser/Applet Viewer that
  14.  * the program is currently running under.
  15.  * it is used in conjunction with class symantec.itools.net.RelativeURL to
  16.  * provide URLs that are relative to an appletÆs or applicationÆs document base.
  17.  * For applets the document base is the URL of the document that the applet is
  18.  * embedded in. For applications the document base is the same as the ôuser.dir
  19.  * system property.
  20.  */
  21. public class Context
  22. {
  23.     private static Applet applet;
  24.     private static URL documentBase;
  25.     private static boolean initialized = false;
  26.  
  27.     /**
  28.      * DonÆt use, this is an all-static class.
  29.      */
  30.     public Context() {
  31.     }
  32.  
  33.     /**
  34.      * Returns true if this is being used with an applet,
  35.      * otherwise this is being used with an application.
  36.      * @see #isApplication
  37.      * @see #getApplet
  38.      */
  39.     public static boolean isApplet()
  40.     {
  41.         return applet != null;
  42.     }
  43.  
  44.     /**
  45.      * Returns true if this is being used with an application,
  46.      * otherwise this is being used with an applet.
  47.      * @see #isApplet
  48.      */
  49.     public static boolean isApplication()
  50.     {
  51.         return applet == null;
  52.     }
  53.  
  54.     /**
  55.      * Notes the applet this class is being used with.
  56.      * @param a the applet to use this class with.
  57.      * @see #getApplet
  58.      */
  59.     public static void setApplet(Applet a)
  60.     {
  61.         applet = a;
  62.         initialized = true;
  63.     }
  64.  
  65.     /**
  66.      * Returns the current applet, or null if being used with an application.
  67.      * @see #setApplet
  68.      */
  69.     public static Applet getApplet()
  70.     {
  71.         return applet;
  72.     }
  73.  
  74.     /**
  75.      * Manually sets the current document base.
  76.      * If the document base is not manually specified it will be automatically
  77.      * determined.
  78.      * @param db the new document base
  79.      * @see #getDocumentBase
  80.      */
  81.     public static void setDocumentBase(URL db)
  82.     {
  83.         // Hack because Netscape Communicator 4.0 Preview 4 for Windows
  84.         // will not look up documents off of the local file system based
  85.         // on the URL returned from java.applet.Applet.getDocumentBase()
  86.         if (System.getProperty("java.vendor").startsWith("Netscape") &&
  87.             db.getHost() == "" &&
  88.             System.getProperty("os.name").startsWith("Windows")) {
  89.  
  90.             String f = db.getFile();
  91.             String hd = f.substring(0, f.indexOf(':') + 1);
  92.             f = f.substring(f.indexOf(':') + 1);
  93.             try {
  94.                 documentBase = new URL(db.getProtocol(), hd, f);
  95.             }
  96.             catch (java.net.MalformedURLException e) { }
  97.         }
  98.  
  99.         else {
  100.             documentBase = db;
  101.         }
  102.  
  103.         initialized = true;
  104.     }
  105.  
  106.     private static void initializeApp()
  107.     {
  108.         StringBuffer p = new StringBuffer(System.getProperty("user.dir"));
  109.         int         pl = p.length();
  110.  
  111.         // If the system file separator isn't the URL file separator convert it.
  112.         try
  113.         {
  114.             char ps = (System.getProperty("file.separator")).charAt(0);
  115.             if(ps != '/')
  116.                 for(int counter = 0; counter < pl; counter++)
  117.                 {
  118.                     if(ps == p.charAt(counter)) p.setCharAt(counter, '/');
  119.                 }
  120.         }
  121.         catch (StringIndexOutOfBoundsException e) { }
  122.  
  123.         try {
  124.             documentBase = new URL("file:///" + p + "/");
  125.         }
  126.         catch (java.net.MalformedURLException e) { }
  127.     }
  128.  
  129.     /**
  130.      * Returns the current document base.
  131.      * For applets the document base is the URL of the document that the
  132.      * applet is embedded in. For applications the document base is the same as
  133.      * the ôuser.dirö system property.
  134.      * @see #setDocumentBase
  135.      */
  136.     public static URL getDocumentBase()
  137.     {
  138.         if (! initialized) {
  139.             initializeApp();
  140.             initialized = true;
  141.         }
  142.  
  143.         if(documentBase != null)
  144.         {
  145.             return documentBase;
  146.         }
  147.         else if (applet != null)
  148.         {
  149.             return applet.getDocumentBase();
  150.         }
  151.  
  152.         return null;
  153.     }
  154. }